package com.qixin.modules.eaApplication.controller;

import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.EaBusinessApplyChange;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyChangeMapper;
import com.qixin.modules.eaApplication.service.IEaBusinessApplyChangeServiceCopy;
import com.qixin.modules.eaApplication.vo.EaBusinessApplyChangePage;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.IdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
* @Description: 出差申请变更单
* @Author: qixin-boot
* @Date:   2021-07-02
* @Version: V1.0
*/
@Api(tags="app出差申请变更单")
@RestController
@RequestMapping("/app/api/eaBusinessApplyChange")
@Slf4j
public class ApiEaBusinessApplyChangeController {
   @Autowired
   private IEaBusinessApplyChangeServiceCopy eaBusinessApplyChangeService;
   @Autowired
   private EaBusinessApplyChangeMapper eaBusinessApplyChangeMapper;

    /**
     * 编辑出差申请变更单信息
     *
     * @param eaBusinessApplyChangePage
     * @return
     */
    @AutoLog(value = "出差申请变更单信息-编辑")
    @ApiOperation(value="出差申请变更单信息-编辑", notes="出差申请变更单信息-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaBusinessApplyChangePage eaBusinessApplyChangePage) {
        EaBusinessApplyChange eaBusinessApplyChange = new EaBusinessApplyChange();
        BeanUtils.copyProperties(eaBusinessApplyChangePage, eaBusinessApplyChange);
        EaBusinessApplyChange eaBusinessApplyChangeEntity = eaBusinessApplyChangeService.getById(eaBusinessApplyChange.getId());
        if(eaBusinessApplyChangeEntity==null) {
            return Result.error("未找到对应数据");
        }

        //判断单据是否已经被提交
        EaBusinessApplyChange eaBusinessApplyChange1 = eaBusinessApplyChangeService.getById(eaBusinessApplyChangePage.getId());
        String status = EmptyUtil.checkNull(eaBusinessApplyChange1.getState());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
        if(status.equals("1") || status.equals("2")){
            return Result.error("单据状态已变更,请刷新");
        }

        eaBusinessApplyChange.setSaveType("add");    //保存
        Map<String,Object> returnMap = eaBusinessApplyChangeService.updateMain(eaBusinessApplyChange,eaBusinessApplyChangePage.getEaBusinessPerDtlList());

        if(returnMap.get("code").equals("-1")){
            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
        }else{
            returnMap.remove("code");
            returnMap.remove("message");
            return Result.OK(returnMap);
        }
    }

    /**
     * 查询出差申请变更单信息列表
     *
     * @param listPageVo
     * @return Result
     */
    @ApiOperation(value="出差申请变更单信息列表-查询", notes="出差申请变更单信息列表-查询")
    @PostMapping(value = "/list")
    public Result<?> list(@RequestBody ListPageVo listPageVo) {
        Map<String,Object> resutlList = eaBusinessApplyChangeService.list(listPageVo);
        return Result.OK(resutlList);
    }

    /**
     * 查询出差申请变更单详情
     *
     * @param idVo
     * @return
     */
    @ApiOperation(value="出差申请变更单详情-查询", notes="出差申请变更单详情-查询")
    @PostMapping(value = "/selectById")
    public Result<?> selectById(@RequestBody IdVo idVo) {
        String id = idVo.getId();
        EaBusinessApplyChange eaBusinessApplyChange = eaBusinessApplyChangeMapper.selectById(id);
        if(eaBusinessApplyChange==null) {
            return Result.error("未找到对应数据");
        }
        Map<String, Object> resutlList = eaBusinessApplyChangeService.selectById(id);
        return Result.OK(resutlList);
    }

    /**
     * 通过id删除出差申请变更单
     *
     * @param idVo
     * @return
     */
    @AutoLog(value = "出差申请变更单信息-通过id删除")
    @ApiOperation(value="出差申请变更单信息-通过id删除", notes="出差申请变更单信息-通过id删除")
    @PostMapping(value = "/delete")
    public Result<?> delete(@RequestBody IdVo idVo) {
        eaBusinessApplyChangeService.delMain(idVo.getId());
        return Result.OK("删除成功!");
    }

    /**
     * 出差申请变更单审批/退回
     *
     * @param eaTaskVo
     * @return
     */
    @AutoLog(value = "出差申请变更单审批/退回")
    @ApiOperation(value="出差申请变更单审批/退回", notes="出差申请变更单审批/退回")
    @PostMapping(value = "/audit")
    public Result<?> edit(@RequestBody EaTaskVo eaTaskVo) {
        Map<String,Object> returnMap = eaBusinessApplyChangeService.audit(eaTaskVo);
        return Result.OK(returnMap);
    }

}
